package com.qfedu.ps;

import com.qfedu.utils.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class PublishSubscribeProducer {
    /***
     * 订阅模式
     * @param args
     */
    public static void main(String[] args)throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        /**
         * 声明交换机
         * 参数1：交换机名称
         * 参数2：交换机类型，fanout、topic、direct、headers
         */
        channel.exchangeDeclare("fanout_exchange", BuiltinExchangeType.FANOUT);

        channel.queueDeclare("fanout_queue_1",true,false,false,null);
        channel.queueDeclare("fanout_queue_2",true,false,false,null);

        //队列绑定交换机
        channel.queueBind("fanout_queue_1","fanout_exchange","");
        channel.queueBind("fanout_queue_2","fanout_exchange","");

        for (int i = 0; i < 40; i++) {
            String message = "发布订阅模式:欢迎来到杭州千锋教育！"+i;

            channel.basicPublish("fanout_exchange","",null,message.getBytes());
        }
        //关闭资源
        channel.close();
        connection.close();


    }


}
